package view;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.LinkedList;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;
import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;

import control.OrderControl;

import java.awt.Color;

public class RegisterPalletPanel extends JPanel {
	private OrderControl oc;
	private JComboBoxAuto productComboBox;
	private JPanel op;
	private JTextField barcodeTextField;
	public RegisterPalletPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		
		this.oc=oc;
		op = this;
		JButton registerBtn = new JButton("Register");
		registerBtn.addActionListener(new registerPallets());
		
		productComboBox = new JComboBoxAuto(new GetProductList());
		
		JLabel lblCustomer = new JLabel("Barcode");
		lblCustomer.setForeground(Color.GREEN);
		JLabel lblProductName = new JLabel("Product name");
		lblProductName.setForeground(Color.GREEN);
		
		barcodeTextField = new JTextField();
		barcodeTextField.setColumns(10);
		
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblProductName)
						.addComponent(lblCustomer))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(registerBtn)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addComponent(barcodeTextField)
							.addComponent(productComboBox, 0, 0, Short.MAX_VALUE)))
					.addContainerGap(306, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCustomer)
						.addComponent(barcodeTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProductName)
						.addComponent(productComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(registerBtn)
					.addContainerGap(197, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
	}
	
	private class GetProductList implements JComboBoxAutoLinkedList{	
		
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getProducts(string);
		}		
	}
	
	private class registerPallets implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String barcode = barcodeTextField.getText();
			String product = productComboBox.getEditor().getItem().toString();
			oc.registerNewPallet(barcode, product);
			oc.addError("Added order");
		}	
	}
}
